package com.thirdgroup.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thirdgroup.controller.base.BaseServlet;
import com.thirdgroup.service.CusInfoService;
import com.thirdgroup.service.impl.CusInfoServiceImpl;
import com.thirdgroup.utils.respond.R;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Map;

@WebServlet("/cusinfo/*")
public class CusInfoServlet extends BaseServlet {
    private CusInfoService cusInfoService=new CusInfoServiceImpl();
    public void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        R r = cusInfoService.selectAll();
        System.out.println("获取了客户列表数据  ");
        System.out.println(r.getData());
        response.getWriter().write(JSONObject.toJSONString(r));
        response.getWriter().flush();
        response.getWriter().close();
    }
    public void updateStatus(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        // 从请求体中获取 JSON 字符串
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        String json = jsonBuilder.toString();

        // 使用 ObjectMapper 将 JSON 字符串转换为 Map 对象
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> data = objectMapper.readValue(json, new TypeReference<Map<String, String>>() {});

        // 获取客户ID和状态参数值
        String customerId = data.get("customerId");
        String status = data.get("status");
        System.out.println("customerId=="+customerId);
        System.out.println("status=="+status);
        // 创建当前日期对象
        Date date = new Date();

        // 创建 SimpleDateFormat 对象，指定日期时间格式
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 使用 SimpleDateFormat 格式化日期时间
        String formattedDate = formatter.format(date);
        R r = cusInfoService.updateState(customerId, status,formattedDate);
        response.getWriter().write(JSONObject.toJSONString(r));
        response.getWriter().flush();
        response.getWriter().close();
    }
    //根据id删除客户
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        // 从请求体中获取 JSON 字符串
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        String json = jsonBuilder.toString();

        // 使用 ObjectMapper 将 JSON 字符串转换为 Map 对象
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> data = objectMapper.readValue(json, new TypeReference<Map<String, String>>() {});

        // 获取客户ID和状态参数值
        //账号名
        String username = data.get("username");
        //密码
        String password = data.get("password");
        //图片
        String profilePicture = data.get("profilePicture");
        //客户名称
        String name = data.get("name");
        //所属行业
        String industry = data.get("industry");
        System.out.println("username=="+username);
        System.out.println("password=="+password);
        System.out.println("profilePicture=="+profilePicture);
        System.out.println("name=="+name);
        System.out.println("industry=="+industry);
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 定义时间格式化器，用于格式化日期时间
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        // 格式化当前时间
        String formattedDateTime = now.format(formatter);
        // 将格式化后的时间字符串添加前缀
        String id = "C" + formattedDateTime;
        System.out.println(id);
        // 创建当前日期对象
        Date date = new Date();
        // 创建 SimpleDateFormat 对象，指定日期时间格式
        SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 使用 SimpleDateFormat 格式化日期时间
        String formattedDate = formatter2.format(date);
        R r = cusInfoService.add(id,username,password,name,industry,formattedDate,profilePicture);
        response.getWriter().write(JSONObject.toJSONString(r));
        response.getWriter().flush();
        response.getWriter().close();
    }
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        // 从请求体中获取 JSON 字符串
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        String json = jsonBuilder.toString();

        // 使用 ObjectMapper 将 JSON 字符串转换为 Map 对象
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> data = objectMapper.readValue(json, new TypeReference<Map<String, String>>() {});

        // 获取客户ID和状态参数值
        String customerId = data.get("customerid");
        System.out.println("customerId=="+customerId);
        R r = cusInfoService.delete(customerId);
        response.getWriter().write(JSONObject.toJSONString(r));
        response.getWriter().flush();
        response.getWriter().close();
    }
    public void detail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("application/json;charset=utf-8");
        // 从请求体中获取 JSON 字符串
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        String json = jsonBuilder.toString();

        // 使用 ObjectMapper 将 JSON 字符串转换为 Map 对象
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, String> data = objectMapper.readValue(json, new TypeReference<Map<String, String>>() {});

        // 获取ID
        String customerid = data.get("customerid");
        System.out.println("根据id查看管理员");
        System.out.println("customerid=="+customerid);

        R r = cusInfoService.detail(customerid);
        System.out.println("根据id查找到数据如下：");
        System.out.println(r.getData());
        response.getWriter().write(JSONObject.toJSONString(r));
        response.getWriter().flush();
        response.getWriter().close();
    }

}
